﻿using System.Collections.Generic;
using System;
using System.Windows;
using ScottPlot.WPF;
using System.Drawing;

namespace CoordinateFitterDemo.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            IList<PointD> imagePoints = new List<PointD>()
            {
                new PointD(0, 0),
                new PointD(500, 0),
                new PointD(1000, 0),
                new PointD(0, 500),
                new PointD(1000, 500),
                new PointD(0, 1000),
                new PointD(500, 1000),
                new PointD(1000, 1000),


                new PointD(300, 300),
                new PointD(300, 700),
                new PointD(500, 500),
                new PointD(700, 700),
                new PointD(700, 300),
            };
            IList<PointD> worldPoints = new List<PointD>()
            {
                new PointD(0, 0),
                new PointD(500, 0),
                new PointD(1000, 0),
                new PointD(0, 500),
                new PointD(1000, 500),
                new PointD(0, 1000),
                new PointD(500, 1000),
                new PointD(1000, 1000),


                new PointD(300, 300),
                new PointD(300, 700),
                new PointD(400, 400),
                new PointD(700, 700),
                new PointD(700, 300),
            };
            TpsTransform tpsTransform = new TpsTransform();
            var result = tpsTransform.Fit(imagePoints, worldPoints);

            // 原始点
            WpfPlot wpfPlot0 = new WpfPlot()
            {
                Width = 200,
                Height = 200
            };
            double[] dataX_img = new double[imagePoints.Count];
            double[] dataY_img = new double[imagePoints.Count];
            for (int i = 0; i < imagePoints.Count; i++)
            {
                dataX_img[i] = imagePoints[i].X;
                dataY_img[i] = imagePoints[i].Y;
            }
            var line_src = wpfPlot0.Plot.Add.Scatter(dataX_img,dataY_img);
            line_src.LineColor = ScottPlot.Color.FromColor(Color.Red);
            line_src.LineWidth = 0;

            // 目标点
            WpfPlot wpfPlot1 = new WpfPlot()
            {
                Width = 200,
                Height = 200
            };
            double[] dataX_world = new double[worldPoints.Count];
            double[] dataY_world = new double[worldPoints.Count];
            for (int i = 0; i < worldPoints.Count; i++)
            {

                dataX_world[i] = worldPoints[i].X;
                dataY_world[i] = worldPoints[i].Y;
            }
            var line_world = wpfPlot1.Plot.Add.Scatter(dataX_world, dataY_world);
            line_world.LineColor = ScottPlot.Color.FromColor(Color.Blue);
            line_world.LineWidth = 0;

            // 结果点阵
            double[] dataX_result = new double[100];
            double[] dataY_result = new double[100];
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    dataX_result[i * 10 + j] = result.Transform(new PointD(i * 100, j * 100)).X;
                    dataY_result[i * 10 + j] = result.Transform(new PointD(i * 100, j * 100)).Y;
                }
            }
            WpfPlot wpfPlot2 = new WpfPlot()
            {
                Width = 200,
                Height = 200
            };
            var line_result = wpfPlot2.Plot.Add.Scatter(dataX_result, dataY_result);
            line_result.LineColor = ScottPlot.Color.FromColor(Color.Green);
            line_result.LineWidth = 0;

            main.Children.Add(wpfPlot0);
            main.Children.Add(wpfPlot1);
            main.Children.Add(wpfPlot2);
        }
    }
}
